package dryven.config.xml;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import dryven.NotImplementedException;
import dryven.config.ApplicationConfiguration;
import dryven.config.ConfigurationException;
import dryven.config.DatabaseConfiguration;
import dryven.config.MailSenderConfiguration;
import dryven.init.DefaultFrontControllerFactory;
import dryven.init.FrontControllerFactory;

public class MemoryApplicationConfiguration implements ApplicationConfiguration {

	private Class<? extends FrontControllerFactory> _factoryClass;
	private Iterable<String> _modelPackages;
	private Iterable<String> _controllerPackages;
	private Iterable<String> _viewPackages;
	private Map<String,DatabaseConfiguration> _databaseConfigurations = new HashMap<String, DatabaseConfiguration>();
	private DatabaseConfiguration _defaultDBConn;
	private MailSenderConfiguration _mailConfig;
	private File _userDataDirectory;

	@Override
	public Class<? extends FrontControllerFactory> getFrontControllerFactoryClass() {
		if(_factoryClass==null) {
			return DefaultFrontControllerFactory.class;
		}
		return _factoryClass;
	}

	@Override
	public Iterable<String> getViewPackages() {
		return _viewPackages;
	}
	
	public void setViewPackages(Iterable<String> packages) {
		_viewPackages = packages;
	}
	
	@Override
	public Iterable<String> getModelPackages() {
		return _modelPackages;
	}
	
	public void setModelPackages(Iterable<String> packages) {
		_modelPackages = packages;
	}

	@Override
	public String getApplicationRootPath() {
		return null;
	}

	@Override
	public String getApplicationRootURLPath() {
		return null;
	}

	@Override
	public DatabaseConfiguration getDatabaseConfigurationByName(String connName)
			throws ConfigurationException {
		return _databaseConfigurations.get(connName);
	}

	@Override
	public Iterable<DatabaseConfiguration> getDatabaseConfigurations() {
		return _databaseConfigurations.values();
	}

	@Override
	public DebugEnvironment getDebugEnvironment() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DatabaseConfiguration getDefaultDatabaseConfiguration() {
		return _defaultDBConn;
	}
	
	public void addDatabaseConfiguration(DatabaseConfiguration cfg, boolean isDefault) {
		_databaseConfigurations.put(cfg.getConnectionName(), cfg);
		if(isDefault || _databaseConfigurations.size()==1) {
			_defaultDBConn = cfg;
		}
	}

	@Override
	public MailSenderConfiguration getMailSenderConfiguration() {
		return _mailConfig;
	}
	
	public void setMailSenderConfiguration(MailSenderConfiguration mailConfig) {
		_mailConfig = mailConfig;
	}

	@Override
	public String getServerName() {
		return null;
	}

	@Override
	public String getCustomConfigValue(String... keys) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getSignSecret() {
		return null;
	}

	public void setFrontControllerFactoryClass(String factoryClassName) {
		try {
			_factoryClass = (Class<? extends FrontControllerFactory>) Class.forName(factoryClassName);
		} catch (Exception e) {
			throw new RuntimeException(String.format("invalid front controller factory classname: %s",factoryClassName),e);
		}
	}

	@Override
	public Iterable<String> getControllerPackages() {
		return _controllerPackages;
	}

	public void setControllerPackages(Iterable<String> packages) {
		_controllerPackages = packages;
	}

	@Override
	public File getUserDataDirectory() {
		return _userDataDirectory;
	}

	public void setUserDataDirectory(String userDataDirectory) {
		_userDataDirectory = new File(userDataDirectory);
	}

	@Override
	public Iterable<String> getClassPathEntries() {
		throw new NotImplementedException();
	}
}
